[
    async function( kuo,Context,context){
        this.$features = await Kuoplus("cms");
        Context.headers["content-type"] = 'application/json; charset=UTF-8';
        let $DATA = Object.assign({},this.$DATA);
        $DATA.code = 0;
        let $_POST = kuo.post?kuo.post:{};
        let $HASH  = "webviewad/"+Md5(kuo.sessionid);
        let UID= 0;
        let shouming = {
            "Banner":"Banner",
            "Chaping":"插屏",
            "Jili":"激励视屏"
            // OFF 1 打开 2 关闭 3 失败
        };
        if($_POST['apptoken']){
            delete($_POST['apptoken']);
        }
        if($_POST['ac']){

            let GAMEID=  Mode("Tools").Int($_POST['gameid']);

            let DQTIME = Mode("Tools").Time();
            let RIQI =  Mode("Tools").Date("Ymd",DQTIME);

            if($_POST['ac'] == "appget"){
                delete($_POST['ac']);
                $HASH +='web';
                let HUODE = await Mem.Get($HASH);
                if(HUODE){
                    $DATA.code = 1;
                    $DATA.data = HUODE;
                    await Mem.Del($HASH);
                }

            }else if($_POST['ac'] == "appset"){
                delete($_POST['ac']);
                $HASH +='app';
                await Mem.Set($HASH,$_POST,180);
                $DATA.code = 1;
            }else if($_POST['ac'] == "webget"){
                delete($_POST['ac']);
                $HASH +='app';
                let HUODE = await Mem.Get($HASH);
                if(HUODE){
                    $DATA.code = 1;
                    $DATA.data = HUODE;
                    await Mem.Del($HASH);
                }
            }else if($_POST['ac'] == "webset"){
                delete($_POST['ac']);
                $HASH +='web';
                await Mem.Set($HASH,$_POST,180);
                $DATA.code = 1;
            }else if($_POST['ac'] == 'gamepai'){  
                /*游戏数据排行
                riqi
                */
                let CHARUDATA =  json_decode( $_POST['data']?$_POST['data']:'{}');
                let baohan = ['int1','int2','float1','float2','atime'];
                let $db = await db('advertising_data');
                let where = {gameid:GAMEID};
                let orders = [];
                if(CHARUDATA['riqi']){
                    where['riqi'] = Mode("Tools").Int(CHARUDATA['riqi']);
                }
 
                for(var m in CHARUDATA){
                    if(baohan.indexOf(m) > -1 ){
                        let zhisi = (""+CHARUDATA[m]).trim();
    
                        if(zhisi == '-'){
                            orders.push(m+' asc');
                        }else{
                            orders.push(m+' desc');
                        }
                    }
                }
                

                if(orders.length <1){
                    orders= ['atime desc'];
                }
                let shuju = await $db.Where(where).ZhiCha('uid,int1,int2,float1,float2,char1,char2').Order(orders.join(',')).Limit(30).Select();
              
                if(shuju){
                    let uidarray = [];
                    for(let mid in shuju){
                        uidarray.push(shuju[mid]['uid']);
                    }

                    $DATA.code = 1;
                    $DATA.data = shuju;
                    $db = await $db.SetTable("user");
                    let users = await $db.ZhiCha('id,nickname,avatar,sex,level').Where({"id IN":uidarray.join(',')}).Select();
                    if(users){

                        for(var min in users){
                            users[min].avatar =
                            pichttp(users[min].avatar==""?"Tpl/login/avatar/"+(users[min]['id']%10)+".png":users[min].avatar);
                            shuju[min] = Object.assign({},shuju[min],users[min]); 
                        }

                    }
                }
               

            }else if($_POST['ac'] == 'gameget'){  
               
                /*游戏数据读取*/
                let $Security = await Mem.Get("session/"+kuo.sessionid);
               
                if($Security && $Security["uid"]){
                    UID= $Security["uid"];
                }
                if(!$Security ){
                    $Security={
                        uid:0,
                        code:0,
                        tuid:0
                    };
                }

                if( UID > 0 ){

                    let $db = await db('advertising_data');
                    let shuju = await $db.Where({uid:UID,gameid:GAMEID}).Find();
                    if(!shuju){
                       let charuid = await $db.Insert({uid:UID,gameid:GAMEID, atime:DQTIME,riqi:RIQI});
                       if(charuid){
                            shuju ={"uid":UID,"gameid":GAMEID,"guid":0,"int1":0,"int2":0,"float1":0.000,"float2":0.000,"char1":"","char2":"","archive1":"","archive2":"","riqi":RIQI,"atime":DQTIME};
                       }
                    }

                    if(!shuju){
                        $DATA.msg = "nodata";
                        Context.body =  JSON.stringify($DATA);
                        return ;
                    }

                    $DATA.code = 1;
                    $DATA.data = shuju;

                }else{
                    $DATA.msg = "gologin";
                    Context.body =  JSON.stringify($DATA);
                    return ;
                }

            }else if($_POST['ac'] == 'gameset'){  
            
                /*游戏数据储存*/
                let CHARUDATA =  json_decode( $_POST['data']?$_POST['data']:'{}');
                let $Security = await Mem.Get("session/"+kuo.sessionid);
                if($Security && $Security["uid"]){
                    UID= $Security["uid"];
                }

                if(!$Security ){
                    $Security={
                        uid:0,
                        code:0,
                        tuid:0
                    };
                }

                if( UID > 0 ){
                    let $db = await db('advertising_data');
                    let shuju = await $db.Where({uid:UID,gameid:GAMEID}).Find();

                    let baohan = ['int1','int2','float1','float2','char1','char2','archive1','archive2'];

                    let UPDATE ={atime:DQTIME,riqi:RIQI};
                    if(!shuju){
                        UPDATE.uid = UID;
                        UPDATE.gameid = GAMEID;
                        for(var m in CHARUDATA){
                            if(baohan.indexOf(m) > -1 ){
                                UPDATE[m] =CHARUDATA[m]; 
                            }
                        }

                        let charuid = await $db.Insert(UPDATE);
                        if(charuid){
                            $DATA.code = 1;
                        }else {
                            $DATA.code = -1;
                        }

                    }else{

                        for(var m in CHARUDATA){
                            if(baohan.indexOf(m) > -1 ){
                                UPDATE[m] =CHARUDATA[m]; 
                            }
                        }
                    
                        let fanhui = await $db.Where({id:shuju['id']}).Update(UPDATE);
                        if(fanhui){
                            $DATA.code = 1;
                        }else {
                            $DATA.code = -1;
                        }
                    }


                }else{

                    $DATA.msg = "gologin";
                    Context.body =  JSON.stringify($DATA);
                    return ;
                }


            }else if($_POST['ac'] == 'quite'){
                await Mem.Del($HASH+'web');
                await Mem.Del($HASH+'app');
                $DATA.code = 1;
            }
        }
        Context.body =  JSON.stringify($DATA);
    }
]